window.onload = function () {
    //获取keywords
    let kh_searchinput = document.getElementById("kh_searchinput")
    let kh_cityshow = document.getElementsByClassName("kh_cityshow")[0]
    //获取id
    let url = window.location.href;
    let paramStr = url.split('?')[1];
    let paramArr = paramStr.split('&');
    let paramObj = {};
    for (let i = 0; i < paramArr.length; i++) {
        let p_item = paramArr[i];
        paramObj[p_item.split('=')[0]] = p_item.split('=')[1]
    }

    $('#kh_up').on('click', function () {
    
        $().ajax({
            url: `https://elm.cangdu.org/v1/pois?city_id=${paramObj.id}&keyword=${kh_searchinput.value}&type=search`,
            method: 'GET',
            success: res => {
                console.log(res);
                kh_cityshow.innerHTML = ''
                if (res.length == 0) {
                    $create('kh_cityshow', 'kh_addressBox',
                        `暂无该地址`
                    )
                }
                if (res.length > 0) {
                    for (let i = 0; i < res.length; i++) {

                        $create('kh_cityshow', 'kh_addressBox',
                            `<span class='kh_addressname'>${res[i].name}</span><br>
                    <span class='kh_address'>${res[i].address}</span>`
                        )
                        let  kh_addressBox=document.getElementsByClassName('kh_addressBox')
                        // for (let i = 0; i < res.length; i++) {
                        //     kh_addressBox[i].addEventListener('click',()=>{
                        //           window.location.href=`/pages/address/newaddress.html?geohash=${res[i].geohash}`

                        //     })
                          
                        // }

                    }
                } else {
                    $create('kh_cityshow', 'kh_addressBox',
                        `请输入地址`
                    )
                  
                }
                // 点击跳转传送地址
                let  kh_addressBox=document.getElementsByClassName('kh_addressBox')
                for (let i = 0; i < res.length; i++) {
                    kh_addressBox[i].addEventListener('click',()=>{
                          window.location.href=`/pages/address/newaddress.html?geohash=${res[i].geohash}`

                    })
                  
                }





            }

        })
    })
}